package controller

import (
	"github.com/duke-git/lancet/v2/convertor"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"kf/models/dao"
	"kf/models/entity"
	"kf/utils/R"
	"net/http"
)

type SysRoleController struct {
	SysRoleDao *dao.SysRoleDao
	Log        *zap.Logger
}

// 获取option
func (sysRoleController *SysRoleController) GetRolesOptions(c *gin.Context) {
	option := sysRoleController.SysRoleDao.Option()
	c.JSON(http.StatusOK, R.ReturnSuccess(option))
}

func (sysRoleController *SysRoleController) GetPage(c *gin.Context) {
	var pageQuery entity.PageQuery
	err := c.ShouldBindQuery(&pageQuery)
	if err != nil {
		c.JSON(http.StatusOK, R.ReturnFailMsg(err.Error()))
		return
	}
	page, err := sysRoleController.SysRoleDao.GetRolePage(pageQuery)
	if err != nil {
		c.JSON(http.StatusOK, R.ReturnFailMsg(err.Error()))
		return
	}
	c.JSON(http.StatusOK, R.ReturnSuccess(page))
}

func (sysRoleController *SysRoleController) GetRoleForm(c *gin.Context) {
	roleIdStr := c.Param("roleId")
	// 尝试将字符串转换为整数
	roleId, err := convertor.ToInt(roleIdStr)
	if err != nil {
		// 如果转换失败，返回错误响应
		c.JSON(http.StatusInternalServerError, "Invalid ID")
		return
	}
	form, err := sysRoleController.SysRoleDao.GetRoleForm(roleId)
	if err != nil {
		c.JSON(http.StatusOK, R.ReturnFailMsg(err.Error()))
		return
	}
	c.JSON(http.StatusOK, R.ReturnSuccess(form))
}

// 新增角色
func (sysRoleController *SysRoleController) AddRole(context *gin.Context) {
	var roleForm entity.RoleForm
	err := context.ShouldBindJSON(&roleForm)
	if err != nil {
		context.JSON(http.StatusInternalServerError, "新增参数失败")
		return
	}
	result, err := sysRoleController.SysRoleDao.SaveRole(roleForm)
	if err != nil {
		context.JSON(http.StatusOK, R.ReturnFailMsg(err.Error()))
		return
	}
	context.JSON(http.StatusOK, R.ReturnSuccess(result))
}

// 编辑角色
func (sysRoleController *SysRoleController) UpdateRole(c *gin.Context) {
	var roleForm entity.RoleForm
	err := c.ShouldBindJSON(&roleForm)
	if err != nil {
		c.JSON(http.StatusInternalServerError, "修改参数失败")
		return
	}
	result, err := sysRoleController.SysRoleDao.SaveRole(roleForm)
	if err != nil {
		c.JSON(http.StatusOK, R.ReturnFailMsg(err.Error()))
		return
	}
	c.JSON(http.StatusOK, R.ReturnSuccess(result))
}

func (sysRoleController *SysRoleController) DeleteRole(c *gin.Context) {
	idStr := c.Param("ids")
	b, err := sysRoleController.SysRoleDao.DeleteRoles(idStr)
	if err != nil {
		c.JSON(http.StatusOK, R.ReturnFailMsg(err.Error()))
		return
	}
	c.JSON(http.StatusOK, R.ReturnSuccess(b))
}

func (sysRoleController *SysRoleController) GetRoleMenuIds(context *gin.Context) {
	roleIdStr := context.Param("roleId")
	b, err := sysRoleController.SysRoleDao.GetRoleMenuIds(roleIdStr)
	if err != nil {
		context.JSON(http.StatusOK, R.ReturnFailMsg(err.Error()))
		return
	}
	context.JSON(http.StatusOK, R.ReturnSuccess(b))
}

func (sysRoleController *SysRoleController) AssignMenusToRole(context *gin.Context) {
	roleIdStr := context.Param("roleId")
	if roleIdStr == "" {
		context.JSON(http.StatusOK, R.ReturnFailMsg("缺少角色id"))
		return
	}
	var menuIds []uint64
	err := context.ShouldBindJSON(&menuIds)
	if err != nil {
		sysRoleController.Log.Error("分配菜单权限给角色失败", zap.Error(err))
		context.JSON(http.StatusOK, R.ReturnFailMsg("分配菜单权限给角色失败"))
		return
	}
	b, err := sysRoleController.SysRoleDao.AssignMenusToRole(roleIdStr, menuIds)
	if err != nil {
		context.JSON(http.StatusOK, R.ReturnFailMsg(err.Error()))
		return
	}
	context.JSON(http.StatusOK, R.ReturnSuccess(b))
}
